package com.xm.dao.background;

import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 商品详细图片
 * @author 康文程
 * @date 2024-1-19 上午 09:39
 */
public class CommodityDetailsDao extends BaseUtil {
    /**
     * 根据商品ID修改商品详细图片
     *
     * @param commodityId   商品ID
     * @param detailsImages 修改后的商品详细图片
     * @return 修改成功的记录数
     */
    public int updateDetailsImagesByCommodityId(Integer commodityId, String detailsImages) {
        // 先查询是否存在数据
        @Language("SQL")
        String selectSql = "SELECT COUNT(*) FROM commodity_details WHERE commodity_id = ?";
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(selectSql, commodityId);

            if (resultSet.next()) {
                int count = resultSet.getInt(1);
                if (count != 0 && detailsImages.isEmpty()) {
                    // 存在数据，进行修改
                    @Language("SQL")
                    String updateSql = "UPDATE commodity_details SET details_images = NULL WHERE commodity_id = ?";
                    return executeUpdate(updateSql, commodityId);
                } else if (count == 0) {
                    // 不存在数据，进行新增
                    @Language("SQL")
                    String insertSql = "INSERT INTO commodity_details (commodity_id, details_images) VALUES (?, ?)";
                    return executeUpdate(insertSql, commodityId, detailsImages);
                } else {
                    // 存在数据，进行修改
                    @Language("SQL")
                    String updateSql = "UPDATE commodity_details SET details_images = ? WHERE commodity_id = ?";
                    return executeUpdate(updateSql, detailsImages, commodityId);
                }
            }

            // 不存在数据，进行新增
            @Language("SQL")
            String insertSql = "INSERT INTO commodity_details (commodity_id, details_images) VALUES (?, ?)";
            return executeUpdate(insertSql, commodityId, detailsImages);
        } catch (SQLException e) {
            throw new RuntimeException("修改商品详细图片失败", e);
        }finally {
            close(resultSet);
        }
    }
    /**
     * 根据商品ID修改商品详细视频
     *
     * @param commodityId   商品ID
     * @param detailsVideo  修改后的商品详细视频
     * @return 修改成功的记录数
     */
    public int updateDetailsVideoByCommodityId(Integer commodityId, String detailsVideo) {
        // 先查询是否存在数据
        @Language("SQL")
        String selectSql = "SELECT COUNT(*) FROM commodity_details WHERE commodity_id = ?";
        ResultSet resultSet = null ;
        try {
            resultSet = executeQuery(selectSql, commodityId);

            if (resultSet.next()) {
                int count = resultSet.getInt(1);
                if (count != 0 && detailsVideo.isEmpty()) {
                    // 存在数据，进行修改
                    @Language("SQL")
                    String updateSql = "UPDATE commodity_details SET details_video = NULL WHERE commodity_id = ?";
                    return executeUpdate(updateSql, commodityId);
                } else if (count == 0 && detailsVideo.isEmpty() ){
                    // 不存在数据，进行新增
                    @Language("SQL")
                    String insertSql = "INSERT INTO commodity_details (commodity_id, details_video) VALUES (?, ?)";
                    return executeUpdate(insertSql, commodityId, detailsVideo);
                }else{
                    // 存在数据，进行修改
                    @Language("SQL")
                    String updateSql = "UPDATE commodity_details SET details_video = ? WHERE commodity_id = ?";
                    return executeUpdate(updateSql, detailsVideo, commodityId);
                }
            }
            // 不存在数据，进行新增
            @Language("SQL")
            String insertSql = "INSERT INTO commodity_details (commodity_id, details_video) VALUES (?, ?)";
            return executeUpdate(insertSql, commodityId, detailsVideo);
        } catch (SQLException e) {
            throw new RuntimeException("修改商品详细视频失败", e);
        }finally {
            close(resultSet);
        }

    }
}
